package com.flyaudio.xiaopengautopilot.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.flyaudio.xiaopengautopilot.R;
import com.flyaudio.xiaopengautopilot.constant.MsgViewUtil;
import com.flyaudio.xiaopengautopilot.presenter.IActivityCallback;
import com.flyaudio.xiaopengautopilot.presenter.IVehicleModule;
import com.flyaudio.xiaopengautopilot.util.L;
import com.flyaudio.xiaopengautopilot.util.TextViewUtils;

import java.text.DecimalFormat;

public class AvgPowerConsumptionDialog extends Dialog implements View.OnClickListener {
    private final String TAG = "AvgPowerConsumptionDialog";

    private Context context;
    private int resLayout;

    private TextView tv_total_power_consumption;
    private TextView tv_month_power_consumption;
    private Button bt_power_consumption_reset;

    public AvgPowerConsumptionDialog(Context context) {
        super(context);
    }

    public AvgPowerConsumptionDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public AvgPowerConsumptionDialog(Context context, int theme, int resLayout) {
        super(context, theme);
        this.context = context;
        this.resLayout = resLayout;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(resLayout);

        Window dialogWindow = getWindow();
        WindowManager manager = ((Activity) context).getWindowManager();
        WindowManager.LayoutParams params = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.BOTTOM);
        initView();
        update();
        L.d(TAG, "onCreate");
    }

    private void initView() {
        tv_total_power_consumption = findViewById(R.id.tv_total_power_consumption);
        tv_month_power_consumption = findViewById(R.id.tv_month_power_consumption);
        bt_power_consumption_reset = findViewById(R.id.bt_power_consumption_reset);
        bt_power_consumption_reset.setOnClickListener(this);

    }

    private void updateTotalPowerConsumption() {
        float totalPowerAvg = IVehicleModule.getInstance().getTotalPowerAvg();
        int pcAvgLength = String.valueOf(totalPowerAvg).length();
        String sHundredkm_unit = String.format(context.getResources().getString(R.string.vehicle_pc_unit), totalPowerAvg);
        SpannableString unitString = TextViewUtils.getSizeColorSpanPx(context,sHundredkm_unit,pcAvgLength,sHundredkm_unit.length(),28, context.getResources().getColor(R.color.colorConditionDrak));
        tv_total_power_consumption.setText(unitString);
    }

    private void updateMonthPowerConsumption() {
        float totalPowerAvg = IVehicleModule.getInstance().getMonthPowerAvg();
        int pcAvgLength = String.valueOf(totalPowerAvg).length();
        String sHundredkm_unit = String.format(context.getResources().getString(R.string.vehicle_pc_unit), totalPowerAvg);
        SpannableString unitString = TextViewUtils.getSizeColorSpanPx(context,sHundredkm_unit,pcAvgLength,sHundredkm_unit.length(),28, context.getResources().getColor(R.color.colorConditionDrak));
        tv_month_power_consumption.setText(unitString);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_power_consumption_reset:
                ResetDialog dialog = new ResetDialog(context, R.style.setDialog, R.layout.dialog_reset);
                dialog.setCanceledOnTouchOutside(false);
                dialog.show();
                break;
        }
    }

    public void update() {
        updateTotalPowerConsumption();
        updateMonthPowerConsumption();
    }

}
